package com.bojin.studio.ubattery;

import java.util.Locale;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.ToggleButton;

public class SettingFragment extends Fragment {
	
	private TextView tvenbale=null;
	private WebView wvAbout=null;
	private CheckBox enableService=null;
	private SharedPreferences sharedPreferences;
	private Button b1=null;
	
    public SettingFragment() {
    	
    }
     
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity()); 
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
    	View view=inflater.inflate(R.layout.fragment_setting, container, false); 
    	setupViewComponent(view);
    	loadVersion();
    	enableService();  	
    	return view; 
    } 
    
    private void setupViewComponent(View view)
    {
    	//wvAbout=(WebView)view.findViewById(R.id.webView1);   
    	tvenbale=(TextView)view.findViewById(R.id.textView2);   
    	enableService=(CheckBox)view.findViewById(R.id.checkBox1);    
    	b1=(Button)view.findViewById(R.id.button1); 
    }
    
    private void loadVersion()
    {
    	 b1.setOnClickListener(new View.OnClickListener() {  
             public void onClick(View v) {  
            	 LayoutInflater inflater = LayoutInflater.from(getActivity());
            	 View login_view = inflater.inflate(R.layout.dialog_version,null);
            	 Builder MyAlertDialog = new AlertDialog.Builder(getActivity());
            	 MyAlertDialog.setTitle(getResources().getString(R.string.t_versioninfo));
            	// MyAlertDialog.setMessage("dddvfff");
            	 MyAlertDialog.setView(login_view);
            	 wvAbout=(WebView)login_view.findViewById(R.id.webView1); 
            	 loadAbout();
            	 DialogInterface.OnClickListener OkClick = new DialogInterface.OnClickListener()
            	 {
            		 public void onClick(DialogInterface dialog, int which) {
            		 
            		 }
            	 };
            	 MyAlertDialog.setNeutralButton(getResources().getString(R.string.ok),OkClick );
            	 MyAlertDialog.show();
             }  
         });  
    }
    
    private void loadAbout()
    {
    	Resources res = getResources();
        Configuration conf = res.getConfiguration();
        WebSettings websettings =wvAbout.getSettings();  
        websettings.setJavaScriptEnabled(true);  
        if (conf.locale.equals(Locale.TRADITIONAL_CHINESE))
        {
        	wvAbout.loadUrl("file:///android_asset/tw_info.html"); 
        }
        else if(conf.locale.equals(Locale.SIMPLIFIED_CHINESE))
        {
        	wvAbout.loadUrl("file:///android_asset/cn_info.html");	 
        }
        else
        {
        	wvAbout.loadUrl("file:///android_asset/info.html");		
        }
    }
    
    private void enableService()
    {
    	if(sharedPreferences.getBoolean("enable_service", true))
    	{
        	tvenbale.setText(R.string.show_on);
        	enableService.setChecked(true);
    	}
    	else
    	{
    		tvenbale.setText(R.string.show_off);
    		enableService.setChecked(false);
    	}
    	
    	enableService.setOnCheckedChangeListener(new OnCheckedChangeListener()
        {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
            {
                if ( isChecked )
                {
                	tvenbale.setText(R.string.show_on);
                	SharedPreferences.Editor editor1=sharedPreferences.edit();
       				editor1.putBoolean("enable_service", true); 
       				editor1.commit();  	
                	getActivity().startService(new Intent(getActivity(),MainService.class));
                }
                else
                {
                	tvenbale.setText(R.string.show_off);
                	SharedPreferences.Editor editor2=sharedPreferences.edit();
       				editor2.putBoolean("enable_service", false); 
       				editor2.commit();  	
       				getActivity().stopService(new Intent(getActivity(),MainService.class));	
                }

            }
        });
    }
}
